package com.chenfan.magic.validator;

import com.chenfan.magic.constant.ParamConstant;
import com.chenfan.magic.constant.RuntimeConstant;
import com.chenfan.magic.core.IdcEngineEntry;
import com.chenfan.magic.entity.Isv;
import com.chenfan.magic.entity.TTLDetailModel;
import com.chenfan.magic.entity.TTLModel;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 主校验器
 *
 * @author 周夕
 * @date 2020-02-26 17:22
 */
@Slf4j
@Data
public class _MainValidator {

    public Map<String, Object> validate(boolean debug, boolean paramSwitch, TTLModel ttlModel, Map<String, Object> idcRuntimePool, Isv isv, String preRequestParams, String tokenArrows) {
        TTLDetailModel summaryModel = new TTLDetailModel();
        summaryModel.start("validator#bus");
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.putAll((Map<String, Object>) IdcEngineEntry.takeRuntimeVar(idcRuntimePool, new RequestParamsValidator().validate(debug, paramSwitch, preRequestParams, isv, ttlModel), RuntimeConstant.RUNTIME_REQUEST_PARAMS));
        paramMap.putAll((Map<String, Object>) IdcEngineEntry.takeRuntimeVar(idcRuntimePool, new TokenArrowsValidator().validate(debug, tokenArrows, isv, ttlModel), RuntimeConstant.RUNTIME_TOKEN_ARROWS));
        paramMap.putAll((Map<String, Object>) IdcEngineEntry.takeRuntimeVar(idcRuntimePool, ParamConstant.get(), RuntimeConstant.RUNTIME_CONSTANTS));
        idcRuntimePool.put(RuntimeConstant.RUNTIME_PARAMS, paramMap);

        summaryModel.end();

        //重新计算时间
        ttlModel.getValidator().put(summaryModel).caculate();
        return paramMap;
    }

    public Map<String, Object> validate(TTLModel ttlModel, Map<String, Object> idcRuntimePool, Isv isv, String preRequestParams, String tokenArrows) {
        return validate(false, false, ttlModel, idcRuntimePool, isv, preRequestParams, tokenArrows);
    }
}
